package com.smartalarmclock.Logic;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Set;

import com.smartalarmclock.Model.ContactFactory;
import com.smartalarmclock.Model.IAutoResponse;
import com.smartalarmclock.Model.IContact;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ISingleContact;
import com.smartalarmclock.Model.ITimeFrame.DayInWeek;
import com.smartalarmclock.Model.ITimeFrame.TimeInWeek;
import com.smartalarmclock.Model.RulesItemFactory;
import com.smartalarmclock.Model.Exceptions.ConflictingSchedueledProfilesException;
import com.smartalarmclock.Model.Exceptions.DataEntityDoesNotExistException;
import com.smartalarmclock.Model.Exceptions.PhoneContactDoesNotExistException;

import android.content.Context;
import android.util.Log;

public class AcAutoResponseService {
	
	public static class AcAutoResponseServiceStatic {
		
		private static final String TAG = "AcAutoResponseServiceStatic";
		public AcAutoResponseServiceStatic() {
			super();
		}

		// TODO: this is just a simple test implementation... Need to handle it correctly (unknown numbers
		// should sometimes also be declined which is currently not convered).
		
		public static IProfile GetRunningProfile(Context i_Context) throws ConflictingSchedueledProfilesException
		{
			IProfile runningProfile = null;
			RulesItemFactory profilesFactory = new RulesItemFactory();
			Calendar c = Calendar.getInstance();
			
			DayInWeek dayInWeek = DayInWeek.FromValue(c.get(Calendar.DAY_OF_WEEK));
			Time time = new Time(c.getTimeInMillis());
			TimeInWeek currentTime = new TimeInWeek(dayInWeek, time);
			
			Log.v(TAG, "Current time: " + currentTime.toString());

			Set<IProfile> matchingProfiles = profilesFactory.GetProfilesByTimeRange(i_Context, currentTime, currentTime);
			
			if (matchingProfiles != null && matchingProfiles.size() > 1) // Only one profile should be active and enabled at a specified time.
			{
				Log.v(TAG, "For some reason, there are more than one profile active at the same time");
				String conflictMessage = ConflictingSchedueledProfilesException.CreateConflictMessage(matchingProfiles, currentTime);
				throw new ConflictingSchedueledProfilesException(conflictMessage);
			}
			
			if (matchingProfiles != null && !matchingProfiles.isEmpty())
			{
				Log.v(TAG, "running profile should be good to go");
				runningProfile = matchingProfiles.iterator().next();
			}
			
			return runningProfile;
		}
		
		public static IAutoResponse GetAutoResponseForContact(String i_PhoneNumber, Context i_Context)
		{
			Log.v(TAG, "Entered getAutoResponseForContact. Phone: " + i_PhoneNumber);
			IProfile runningProfile = null;
			IAutoResponse autoResponse = null;
			try {
				runningProfile = GetRunningProfile(i_Context);
			} catch (ConflictingSchedueledProfilesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			ISingleContact contactCalling = null;
			ContactFactory contactFactory = new ContactFactory();
			try {
				contactCalling = contactFactory.GetSingleContactByPhoneNumber(i_Context, i_PhoneNumber);
			} catch (DataEntityDoesNotExistException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (PhoneContactDoesNotExistException e1) {
				Log.v(TAG, "Contact doesn't exist in phonebook");
				contactCalling = null;
			}
			
			if (runningProfile != null)
			{
				if (contactCalling == null || !runningProfile.GetContacts(i_Context).contains(contactCalling))
				{
					// For debug?
					String message = contactCalling == null ? "contact doesn't appear in phone" :
						"contact is not part of running profile...";
					Log.v(TAG, message);
					autoResponse = runningProfile.GetNonMatchingAutoResponse();
				}
				else
				{
					autoResponse = runningProfile.GetMatchingAutoResponse();
				}
			}
			
			return autoResponse;
		}	
	}
}
